//
//  LocationToolObject.m
//  DDSocial
//
//  Created by admin on 2018/9/28.
//  Copyright © 2018年 XY. All rights reserved.
//

#import "SystemPermissionsToolObject.h"
#import <CoreLocation/CLLocationManager.h>
#import <UserNotifications/UserNotifications.h>
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
//#import <AddressBook/AddressBook.h>
//#import <Contacts/Contacts.h>
//#import <CoreBluetooth/CoreBluetooth.h>
//#import <CoreTelephony/CTCellularData.h>
//#import <MediaPlayer/MediaPlayer.h>


@implementation SystemPermissionsToolObject

///是否开启定位
+ (void)openLocationServiceWithBlock:(ReturnBlock)returnBlock
{
    BOOL isOpen = NO;
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {
        isOpen = YES;
    }
    if (returnBlock) {
        returnBlock(isOpen);
    }
}

///是否允许消息推送
+ (void)openMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock
{
    BOOL isOpen = NO;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
    UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    if (setting.types != UIUserNotificationTypeNone) {
        isOpen = YES;
    }
#else
    UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (type != UIRemoteNotificationTypeNone) {
        isOpen = YES;
    }
#endif
    if (returnBlock) {
        returnBlock(isOpen);
    }
}

///是否开启摄像头
+ (void)openCaptureDeviceServiceWithBlock:(ReturnBlock)returnBlock
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (returnBlock) {
                returnBlock(granted);
            }
        }];
        //        return NO;
    } else if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
        returnBlock(NO);
    } else {
        returnBlock(YES);
    }
#endif
}

///是否开启相册
+ (void)openAlbumServiceWithBlock:(ReturnBlock)returnBlock
{
    BOOL isOpen;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
    PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
    isOpen = YES;
    if (authStatus == PHAuthorizationStatusRestricted || authStatus == PHAuthorizationStatusDenied) {
        isOpen = NO;
    }
#else
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    isOpen = YES;
    if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {
        isOpen = NO;
    }
#endif
    if (returnBlock) {
        returnBlock(isOpen);
    }
}

+ (void)determineLatestPermissionsOfTheLocalAlbum {
    if (@available(iOS 14, *)) {
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite];
        if (status == PHAuthorizationStatusLimited) {
            //只展示选中的照片
            DebugLog(@"limited");
            [DCSpeedy dc_SetUpSingleAlterWithView:[DCSpeedy getcurrentTopViewController] Title:@"警告" Message:@"检测到您开启的照片权限只会展示选中的照片,并不会展示所有的照片,是否前往设置修改照片权限?" tureTitle:@"立即前往" Sure:^{
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url];
                }
            }];
            
        } else if (status == PHAuthorizationStatusDenied) {
            //不允许(无)
            DebugLog(@"denied");
        } else if (status == PHAuthorizationStatusAuthorized) {
            //所有照片
            DebugLog(@"authorized");
        } else {
            
        }
        
    } else {
        // Fallback on earlier versions
    }
}

///是否开启通讯录
//+ (void)openContactsServiceWithBolck:(ReturnBlock)returnBolck
//{
//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
//    CNAuthorizationStatus cnAuthStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
//    if (cnAuthStatus == CNAuthorizationStatusNotDetermined) {
//        CNContactStore *store = [[CNContactStore alloc] init];
//        [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError *error) {
//            if (returnBolck) {
//                returnBolck(granted);
//            }
//        }];
//    } else if (cnAuthStatus == CNAuthorizationStatusRestricted || cnAuthStatus == CNAuthorizationStatusDenied) {
//        if (returnBolck) {
//            returnBolck(NO);
//        }
//    } else {
//        if (returnBolck) {
//            returnBolck(YES);
//        }
//    }
//#else
//    //ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
//    ABAddressBookRef addressBook = ABAddressBookCreate();
//    ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
//    if (authStatus != kABAuthorizationStatusAuthorized) {
//        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                if (error) {
//                    NSLog(@"Error: %@", (__bridge NSError *)error);
//                    if (returnBolck) {
//                        returnBolck(NO);
//                    }
//                } else {
//                    if (returnBolck) {
//                        returnBolck(YES);
//                    }
//                }
//            });
//        });
//    } else {
//        if (returnBolck) {
//            returnBolck(YES);
//        }
//    }
//#endif
//}
//
/////是否开启蓝牙
//+ (void)openPeripheralServiceWithBolck:(ReturnBlock)returnBolck {
//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
//    CBPeripheralManagerAuthorizationStatus cbAuthStatus = [CBPeripheralManager authorizationStatus];
//    if (cbAuthStatus == CBPeripheralManagerAuthorizationStatusNotDetermined) {
//        if (returnBolck) {
//            returnBolck(NO);
//        }
//    } else if (cbAuthStatus == CBPeripheralManagerAuthorizationStatusRestricted || cbAuthStatus == CBPeripheralManagerAuthorizationStatusDenied) {
//        if (returnBolck) {
//            returnBolck(NO);
//        }
//    } else {
//        if (returnBolck) {
//            returnBolck(YES);
//        }
//    }
//#endif
//}
//
/////检测是否开启联网
//+ (void)openEventServiceWithBolck:(ReturnBlock)returnBolck
//{
//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
//    CTCellularData *cellularData = [[CTCellularData alloc] init];
//    cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){
//        if (state == kCTCellularDataRestrictedStateUnknown || state == kCTCellularDataNotRestricted) {
//            if (returnBolck) {
//                returnBolck(NO);
//            }
//        } else {
//            if (returnBolck) {
//                returnBolck(YES);
//            }
//        }
//    };
//    CTCellularDataRestrictedState state = cellularData.restrictedState;
//    if (state == kCTCellularDataRestrictedStateUnknown || state == kCTCellularDataNotRestricted) {
//        if (returnBolck) {
//            returnBolck(NO);
//        }
//    } else {
//        if (returnBolck) {
//            returnBolck(YES);
//        }
//    }
//#endif
//}
//
//
/////开启媒体资料库/Apple Music 服务
//+ (void)openMediaPlayerServiceWithBlock:(ReturnBlock)returnBlock
//{
//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_3
//    if (@available(iOS 9.3, *)) {
//        MPMediaLibraryAuthorizationStatus authStatus = [MPMediaLibrary authorizationStatus];
//        if (authStatus == MPMediaLibraryAuthorizationStatusNotDetermined) {
//            [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status) {
//                if (status == MPMediaLibraryAuthorizationStatusAuthorized) {
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        if (returnBlock) {
//                            returnBlock(YES);
//                        }
//                    });
//                }else{
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        if (returnBlock) {
//                            returnBlock(NO);
//                        }
//                    });
//                }
//            }];
//        }else if (authStatus == MPMediaLibraryAuthorizationStatusAuthorized){
//            if (returnBlock) {
//                returnBlock(YES);
//            }
//        }else{
//            if (returnBlock) {
//                returnBlock(NO);
//            }
//        }
//    } else {
//        // Fallback on earlier versions
//    }
//#endif
//}
@end
